define(
		[ "AppPathDir/product/reserveInfos/addVisitor", "AppPathDir/product/reserveInfos/selectVisitor", "PDUtilDir/dataService", "PDAppDir/properties", "PDUtilDir/util",
				"css!AppPathDir/product/reserveInfos/views/css/css" ],
		function( AddVisitorSupport, SelectVisitorSupport, DataService, Properties, Util, Moment) {
			HomeApp.controller('BodyCtrl',["$scope", "$compile", '$sce', function($scope, $compile, $sce) {
				$scope.entity = {
					visitor : []
				};
				$scope.cache = $.extend({
					grid : undefined,
					visitor: new Array(),
					seats: new Array()
				}, DataService.getObject('product.search.reserve'));
				$scope.bind = {
					seats: new Map()
				};


                DataService.setObject('product.search.reserve.visitors.seat', []);

				$scope.cache.entity.notes_str_obj = $sce.trustAsHtml($scope.cache.entity.notes_str);
				
				$.ajax({
					url : getServer() + "/flea/hclv/getCertificates.htmls",
					success : function(data) {
						$scope.cache.certificates = data.data;
						var certificates = data.data, len = certificates.length, obj = new Map();
						for(var i = 0; i < len; i++){
							obj.set(certificates[i].dictCode, certificates[i].dictName);
						}
						$scope.cache.certificatesObject = obj;
						$scope.$digest();
					}
				});
				// 站位
				$.ajax({
					url : getServer() + "/flea/hclv/car/visitor/createCarSeat.htmls",
					data : {
						timeTableId : $scope.cache.timeTable.id
					},
					success : function(data) {
						if(data.status == 200){
							var allData = data.data;

							if(JSON.stringify(allData)=='{}')
								return;
							
							var tmpDataArray = undefined, newArray = new Array(), tmpArray = new Array();
							for ( var key in allData) {
								tmpDataArray = allData[key];
                                newArray = new Array();
                                tmpArray = new Array();
								for(var i = 0; i < 57; i++){
									tmpArray.push(tmpDataArray[i]);
									if(i == 55){
										continue;
									}
									if(i % 4 == 3 && tmpArray.length > 0){
										newArray.push(tmpArray);
										tmpArray = new Array();
									}
								}
								newArray.push(tmpArray);
								$scope.bind.seats[key] = newArray;
							}
							// console.info($scope.bind.seats);
							$scope.$digest();
						}
					}
				});
				
				$scope.addVisitorSupport = AddVisitorSupport($scope, $compile);
				$scope.selectVisitorSupport = SelectVisitorSupport($scope, $compile);

				// 表单校验
				$scope.validFun = function(number) {
					var selecteds = $scope.cache.seats, seatsLen = selecteds.length;
					
					if(seatsLen == 0 ){
						$scope.productPayOrderForm.isOk.$setViewValue(undefined);
						return;
					}
					var visitors = $scope.cache.visitor, visitorLen = visitors.length;
					
					if(seatsLen != visitorLen){
						$scope.productPayOrderForm.isOk.$setViewValue(undefined);
						return;
					}
					$scope.productPayOrderForm.isOk.$setViewValue(true);
				};
				
				// 刷新游客列表
				$scope.reloadSupport = {
					run : function() {
						var selecteds = $scope.cache.visitor,
							len = selecteds.length;
							i = 0,
							datas = DataService.getObject('product.search.reserve.visitors');
						$.each(datas, function(index, row) {
							len = selecteds.length;
							for(i = 0; i < len; i++){
								if (row.id == selecteds[i].id)
									break;
							}
							if(i == len)
								selecteds.push(row);
						});
						$scope.validFun();
						$scope.$digest();
					}
				};
				
				// 刷新座位列表
				$scope.refreshSeat = function(carNumber, seatNumber) {
					var isUse = 0, col = 0;
					
					if(!carNumber && !seatNumber){
						isUse = 1;
						carNumber = $scope.bind.seat.carNumber;
						seatNumber = $scope.bind.seat.seatNumber;
					}

					selecteds = $scope.bind.seats[carNumber];
                    if(seatNumber == 57)
						selecteds = selecteds[13];
                    else{
						col = parseInt(seatNumber / 4);
						if(seatNumber % 4 > 0){
							col ++;
						}
                        selecteds = selecteds[col - 1];
					}

					$.each(selecteds, function(index, row) {
						if (seatNumber == row.seatNumber){
							row.isUse = isUse;
							return;
						}
					});
				};
				// 刷新游客对应的座位
				$scope.refreshVisitorSeat = function(data) {
					var selecteds = $scope.cache.seats,
						len = selecteds.length,
						i = 0;

					for(i = 0; i < len; i++){
						if (data.id == selecteds[i].id){
							// console.info(selecteds[i].carNumber + '-------------' + selecteds[i].seatNumber)
							$scope.refreshSeat(selecteds[i].carNumber, selecteds[i].seatNumber);
							if($scope.bind.seat){
                                selecteds[i].seatId = $scope.bind.seat.seatId;
								selecteds[i].carNumber = $scope.bind.seat.carNumber;
								selecteds[i].seatNumber = $scope.bind.seat.seatNumber;
							}
							break;
						}
					}
					if(!$scope.bind.seat){
						selecteds.splice(i, 1);
						return;
					}
					if(i == len){
                        selecteds.push($.extend(data, {
                            seatId: $scope.bind.seat.seatId,
                            carNumber: $scope.bind.seat.carNumber,
                            seatNumber: $scope.bind.seat.seatNumber
                        }));
					}
                    var selecteds = $scope.cache.visitor;

                    $.each(selecteds, function(index, row) {
                        if (row.id == data.id){
                            row.carNumber = $scope.bind.seat.carNumber;
                            row.seatNumber = $scope.bind.seat.seatNumber;
                        }
                    });
					$scope.refreshSeat();
				};
				
				// 存储座位列表
				$scope.reloadSeatSupport = {
					run : function() {
						var datas = DataService.getObject('product.search.reserve.visitors.seat');
						$scope.refreshVisitorSeat(datas[0]);
						$scope.bind.seat = undefined;
						$scope.validFun();
						$scope.$digest();
					}
				};
				
				$scope.removeVisitor = function(id) {

					var data = $scope.cache.visitor,
						len = data.length,
						i = 0,
						tmpData = undefined;

					for (i = 0; i < len; i++) {
						if (id == data[i].id) {
                            tmpData = data[i];
							break;
						}
					}

                    if(tmpData){
                        Util.confirm("确认移除游客 : " + tmpData.zhName + "?", function() {
                            $scope.refreshVisitorSeat(tmpData);
                            data.splice(i, 1);
                            $scope.validFun();
                        });
                    }
				};
				
				$scope.payFun = function() {
					var datas = $scope.cache.seats, seats = new Array();
					$.each(datas, function(index, row) {
						seats.push({
                            seatId: row.seatId,
							carNumber: row.carNumber,
							seatNumber: row.seatNumber,
                            visitorId: row.id
						});
					});
					$.ajax({
						url : getServer()
								+ "/flea/hclv/visitor/order/loadOrder.htmls",
						method : 'post',
						data : {
							model : JSON.stringify({
								addVisitors : $scope.cache.visitor,
								seats: seats,
								timeTableId : $scope.cache.timeTable.id
							})
						},
						success : function(data) {
							if (data.status != 200) {
								return;
							}
							$scope.cache.visitor = data.data;
                            if(window.top.tabs.getTab('order'))
                            	window.top.tabs.closeTab('order');

							window.parent.location.href = getServer() + '/flea/home.htmls#/order';
							window.top.tabs.closeTab($scope.cache.entity.product.id + '_' + $scope.cache.timeTable.id + '_reserve');
						}
					});
				};
			}
	]);
			HomeApp.filter('to_trusted', ['$sce', function ($sce) {
			　　  return function (text) {
				  	var arry = text.split(','), number = arry[0], 
				  		isSelected = arry[1],
				  		str = '<a href="javascript:angular.element(\'#testabcdef\').scope().selectVisitorSupport.run(';
				  	str += "'" + arry[0] + "'";
				  	str += ",'" + arry[1] + "'";
				  	str += ",'" + arry[2] + "'";
                    str += ",'" + arry[3] + "'";
				  	str += ');">' + arry[2] + '</a>';
		    　　  	return $sce.trustAsHtml(str);
			　　};
			}]);
});